package com.zoe.wan.android.example.repositry

import android.content.Context
import android.content.Intent
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.ToastUtils
import com.blankj.utilcode.util.Utils
import com.zoe.wan.android.example.activity.login.LoginActivity
import com.zoe.wan.android.example.repositry.data.home.HomeBannerData
import com.zoe.wan.android.example.repositry.data.home.HomeListData
import com.zoe.wan.android.example.repositry.data.knowledge.KnowledgeDetialData
import com.zoe.wan.android.example.repositry.data.knowledge.KnowledgeListData
import com.zoe.wan.android.example.repositry.data.login.UserData
import com.zoe.wan.android.http.BaseResponse
import com.zoe.wan.android.http.RetrofitClient
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.lang.ref.WeakReference

object Repository {

    private const val SUCCESS_CODE = 0
    private const val NEED_LOGIN_CODE = -1001

    // 防止内存溢出
    private var mContext: WeakReference<Context>? = null

    /*
    * 初始化
    * */
    fun init(context: Context) {
        mContext = WeakReference(context)
    }

    suspend fun getHomeList(pageCount: String): HomeListData? {
        val data: BaseResponse<HomeListData>? =
            getDefault(ApiService::class.java).homeList(pageCount)
        return responseCode(data)
    }

    suspend fun getBannerList(): HomeBannerData? {
        val data: BaseResponse<HomeBannerData>? =
            getDefault(ApiService::class.java).homeBannerListData()
        return responseCode(data)
    }

    suspend fun login(userName: String, password: String): UserData? {
        val data: BaseResponse<UserData>? =
            getDefault(ApiService::class.java).login(userName, password)
        return responseCode(data)
    }

    suspend fun register(userName: String, password: String, repassword: String): UserData? {
        val data: BaseResponse<UserData>? =
            getDefault(ApiService::class.java).register(userName, password, repassword)
        return responseCode(data)
    }

    suspend fun collect(id: String): Any? {
        val data: BaseResponse<Any>? =
            getDefault(ApiService::class.java).collect(id)
        return responseCode(data)
    }

    suspend fun logout(): Boolean? {
        val data: BaseResponse<Any>? =
            getDefault(ApiService::class.java).logout()
        if (data?.getData() != null) {
            return data.getErrCode() == 0
        }
        return null
    }

    suspend fun knowledgeList(): KnowledgeListData? {
        val data: BaseResponse<KnowledgeListData>? =
            getDefault(ApiService::class.java).knowledgeList()
        return responseCode(data)
    }

    suspend fun knowledgeListDetail(pageCount: String,cid:String): KnowledgeDetialData? {
        val data: BaseResponse<KnowledgeDetialData>? =
            getDefault(ApiService::class.java).knowledgeListDetail(pageCount,cid)
        return responseCode(data)
    }
    /*
    *  code 0 返回业务数据
    *  code -1001 跳转登录
    *
    * */

    private fun <T> responseCode(response: BaseResponse<T>?): T? {
        if (response == null) {
            showToast(null)
            return null
        } else {
            if (response.getErrCode() == SUCCESS_CODE) {
                return response.getData()
            } else if (response.getErrCode() == NEED_LOGIN_CODE) {
                // 需要登录
                mContext?.get()?.let {
                    val intent = Intent(it, LoginActivity::class.java)
                    intent.putExtra(LoginActivity.Intent_type_name, 0)
                    it.startActivity(intent)
                }
                return null
            } else {
                showToast(response.getErrMsg())
            }
            return null
        }
    }

    fun showToast(msg: String?) {
        GlobalScope.launch(Dispatchers.Main) {
            ToastUtils.showLong(msg ?: "请求异常")
        }
    }

    private fun <T> getDefault(interfaceServer: Class<T>?): T {
        return RetrofitClient.getInstance().getDefault(interfaceServer)
    }
}